package com.xrj4j.k_tally.view

import android.content.Context
import android.widget.Toast
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.commandiron.wheel_picker_compose.WheelDateTimePicker
import com.commandiron.wheel_picker_compose.core.TimeFormat
import com.commandiron.wheel_picker_compose.core.WheelPickerDefaults
import com.xrj4j.k_tally.ui.theme.K_tallyTheme
import java.time.LocalDateTime

fun toast(context: Context, content: String) {
    Toast.makeText(context, content, Toast.LENGTH_SHORT).show()
}

@Composable
fun DatePicker(
    show: Boolean = true,
    initData: LocalDateTime = LocalDateTime.now(),
    onDateSelected: (LocalDateTime) -> Unit = {},
    onCanceled: () -> Unit = {}
) {
    if (show) {
        Dialog(onDismissRequest = { onCanceled() }) {
            WheelDateTimePicker(
                minDateTime = LocalDateTime.of(
                    2015, 1, 1, 0, 0
                ),
                startDateTime = initData,
                maxDateTime = LocalDateTime.of(
                    2050, 1, 1, 0, 0
                ),
                timeFormat = TimeFormat.HOUR_24,
                size = DpSize(200.dp, 100.dp),
                rowCount = 5,
                textStyle = MaterialTheme.typography.titleSmall,
                selectorProperties = WheelPickerDefaults.selectorProperties(
                    enabled = true,
                    shape = RoundedCornerShape(8.dp),
                    color = Color(0xFFf1faee).copy(alpha = 0.2f),
                    border = BorderStroke(2.dp, Color(0xFFf1faee))
                )
            ) { snappedDateTime ->
                onDateSelected(snappedDateTime)
                // onCanceled()
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
fun CommonPreview() {
    K_tallyTheme {
        DatePicker()
    }
}